package middle.id_1_100;

import java.nio.charset.StandardCharsets;
import java.util.*;

public class hua18 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        DhcpServer dhcpServer = new DhcpServer();
        int operationCnt = cin.nextInt();
        for (int i = 0; i < operationCnt; i++) {
            String[] operation = cin.next().split("=");
            if ("REQUEST".equals(operation[0])) {
                System.out.println(dhcpServer.request(operation[1]));
            } else {
                dhcpServer.release(operation[1]);
            }
        }
        cin.close();
    }

    static class DhcpServer {
        // 创建一个最小堆
        private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        private int maxIP=-1;
        Map<String,Integer> macMap=new HashMap<>(); // 记录mac是否出现过  0 代表释放了，1代表已分配
        Map<String,Integer> macIP=new HashMap<>();  //记录mac与ip的配对关系
        int[] ip_state=new int[256];// 0可用  1不可用
        DhcpServer() {
            // 在此补充你的代码
            for(int i=0;i<256;i++){
                ip_state[i]=0;  //初始化0 可用
            }
        }

        String request(String mac) {
            if(macMap.containsKey(mac)) {  // mac出现过
                if(macMap.get(mac)==0) {  // 这个mac 再申请
                    if(ip_state[macIP.get(mac)]==0){ // 如果这个ip还能用
                        ip_state[macIP.get(mac)]=1;
                        macMap.put(mac,1);
                        return "192.168.0."+String.valueOf(macIP.get(mac));
                    }else{  //这个ip不能用了
                        if(!minHeap.isEmpty()){ //已释放的还有ip
                            int min=minHeap.poll();
                            ip_state[min]=1;
                            String ip="192.168.0."+String.valueOf(min);
                            macIP.put(mac,min);
                            macMap.put(mac,1);
                            return "192.168.0."+String.valueOf(macIP.get(mac));
                        }else{
                            return "NA";
                        }
                    }
                }else {
                    macMap.put(mac,1);
                    return "192.168.0."+String.valueOf(macIP.get(mac));
                }
            }else { //来了一个新的mac
                if(maxIP<255){ // 有新的
                    maxIP++;
                    ip_state[maxIP]=1;
                    macIP.put(mac,maxIP);
                    macMap.put(mac,1);
                    return "192.168.0."+String.valueOf(macIP.get(mac));
                }else{ // 没新的ip了
                    if(!minHeap.isEmpty()){ //已释放的还有ip
                        int min=minHeap.poll();
                        ip_state[min]=1;
                        String ip="192.168.0."+String.valueOf(min);
                        macIP.put(mac,min);
                        macMap.put(mac,1);
                        return "192.168.0."+String.valueOf(macIP.get(mac));
                    }else{ // 失败
                        return "NA";
                    }
                }

            }

        }

        void release(String mac) {
            // 在此补充你的代码
            Integer ip=macMap.get(mac);
            if(ip!=null && ip==1){
                macMap.put(mac,0);
                ip_state[macIP.get(mac)]=0;
            }


        }
    }

    static class DhcpServer2 {
        private final String[] ipMap = new String[256];
        private final boolean[] ipUsed = new boolean[256];
        private final Map<String, Integer> usingIp = new HashMap<>();
        private final Map<String, Integer> lastRecentlyUsedIp = new HashMap<>();

        public DhcpServer2() {
            for (int i = 0; i < 256; i++) {
                ipMap[i] = "";
            }
        }

        public String request(String mac) {
            if (usingIp.containsKey(mac)) {
                return getIp(usingIp.get(mac));
            }

            if (lastRecentlyUsedIp.containsKey(mac) && ipMap[lastRecentlyUsedIp.get(mac)].isEmpty()) {
                int ipTail = lastRecentlyUsedIp.get(mac);
                useIp(mac, ipTail);
                return getIp(ipTail);
            }

            for (int i = 0; i < 256; i++) {
                if (ipMap[i].isEmpty() && !ipUsed[i]) {
                    useIp(mac, i);
                    return getIp(i);
                }
            }

            for (int i = 0; i < 256; i++) {
                if (ipMap[i].isEmpty() && ipUsed[i]) {
                    useIp(mac, i);
                    return getIp(i);
                }
            }

            return "NA";
        }

        public void release(String mac) {
            if (usingIp.containsKey(mac)) {
                int ip = usingIp.get(mac);
                ipMap[ip] = "";
                usingIp.remove(mac);
                lastRecentlyUsedIp.put(mac, ip);
            }
        }

        private static String getIp(int tail) {
            return "192.168.0." + tail;
        }

        private void useIp(String mac, int ipTail) {
            ipMap[ipTail] = mac;
            ipUsed[ipTail] = true;
            usingIp.put(mac, ipTail);
        }
    }


}
